package san.entity.m;

import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

import com.google.common.collect.Lists;
import com.orm.BaseEntity;


@Entity
@Table(name = "meal_variety")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Variety extends BaseEntity {

	/**
	 * 品种
	 */
	private static final long serialVersionUID = 5453137844330283047L;
	private String name;
	@OneToMany(mappedBy = "variety", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
	@OrderBy(value = "insertDate")
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	private List<Product> products = Lists.newArrayList();//有序的关联对象集合

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public List<Product> getProducts() {
		return products;
	}

	public void setProducts(List<Product> products) {
		this.products = products;
	}

	/*
	 * 添加订单
	 */
	public void addProduct(Product product) {
		if (!this.products.contains(product)) {
			this.products.add(product);
			product.setVariety(this);
		}
	}
}
